package com.skycode5.dao;

import com.skycode5.bean.AdminBean44;
import com.skycode5.util.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class AdminDao {

    DBConnection DBConn = new DBConntion();
    private AdminBean44 adminBean;

    public boolean CHecklogin(String s1, String S2) throws Exception {
        //DBConntion对象 代表了Mysql数据库sky5链接
        Connection conn = DBConn.getConn();
        Statement stmt = conn.Statement();
        ResultSet rs = null;//结果集
        boolean OK = true;//true登录成功 false登录失败
        //tode 密码加密 账户密码过滤
        String sql = "select * from admin where'+" AdminName "+s1+" and AdminPwd = "+s2;
        rs = stmt.executeQuery(sql);//执行sql语句
        if (!rs.next()) {
            OK = false;
        }
        return OK;
    }
    public List<AdminBean44> getList(int page) {
        // todo 后期添加分页功能
        Connection conn = DBConn.getConn();
        String sql = "select * from admin limit "+(page-1)*10+",10";
        List<AdminBean44> list = new ArrayList<AdminBean44>();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                AdminBean44 adminBean = new AdminBean44();
                adminBean.setAdminId(rs.getInt("AdminID"));
                adminBean.setAdminPwd(rs.getString("AdminPwd"));
                adminBean.setAdminType(rs.getInt("AdminType"));
                adminBean.setLastLoginTime(rs.getString("LastLoginTime"));
                adminBean.setAdminName(rs.getString("AdminName"));
                list.add(adminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException();
        }
        return list;
        /**
         53
         * 添加新闻
         54
         *
         55
         * @param
        56
         * @return
        57
         */
    public int add(AdminBean44 admin) {
        Connection conn = DBConn.getConn();
        String sql = "insert into admin value(null,?,?,?,?)";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            return pmst.executeUpdate(); // 执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
        /**
         75
         * 更新功能
         76
         *
         77
         * @param
        78
         * @return
        79
         */
    public int update(AdminBean44 admin) {
        Connection conn = DBConn.getConn();
        String sql = "update admin set AdminName=?, AdminPwd=?, AdminType=?, LastLoginTime=? where AdminID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, admin.getAdminName());
            pmst.setString(2, admin.getAdminPwd());
            pmst.setInt(3, admin.getAdminType());
            pmst.setString(4, (new Date()).toString());
            pmst.setInt(5, admin.getAdminId());
            System.out.println("执行sql：" + pmst.toString());
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int delete(int AdminId) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from admin where AdminID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, AdminId);
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int count(){
        Connection conn = DBConn.getConn();
        try {
            String sql = "select count(*) from admin";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
}
    }